//
//  videoTableViewCell.m
//  A Learning Walk
//
//  Created by dllo on 16/6/20.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "videoTableViewCell.h"
#import "ZYPlayerViewController.h"
@interface videoTableViewCell ()

@property(nonatomic, retain)UIVisualEffectView *effect;

@end

@implementation videoTableViewCell
-(void)dealloc{
    [_model release];
    [_imageP release];
    [_imageV release];
    [_labelT release];
    [super dealloc];
}
//为了保证同一时间只有一个播放器，使用单例模式
+ (AVPlayerViewController *)sharedInstance{
    static AVPlayerViewController *vc = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vc = [AVPlayerViewController new];
    });
    return vc;
}



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatCell];
    }
    return self;
}
-(void)creatCell{
    self.imageV = [[UIImageView alloc] init];
    self.imageV.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.imageV];
    
    self.touchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.touchButton setImage:[UIImage imageNamed:@"122"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.touchButton];
    [self.touchButton addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.stopButton setImage:[UIImage imageNamed:@"122"] forState:UIControlStateNormal];
//    [self.contentView addSubview:self.stopButton];
//    [self.stopButton addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.labelT = [FengLabel LabelwithFrame:CGRectMake(0, 0, 0, 0) Color:[UIColor clearColor] font:17];
    [self.imageV addSubview:self.labelT];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effect.alpha = 1;
    [self.labelT addSubview:self.effect];
    
    [self.imageV release];
    [self.imageP release];
    [self.labelT release];
}


-(void)touchAction:(UIButton *)button{
    //NSLog(@"12456");
    [[ZYPlayerViewController shareZYPlayerViewController].player stop];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"0" forKey:@"play"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playndStop" object:dic];    
    NSURL *string = [NSURL URLWithString:self.str];
    AVPlayer *player=[AVPlayer playerWithURL:string];
    [player play];
    [videoTableViewCell sharedInstance].player = player;
    [button addSubview:[videoTableViewCell sharedInstance].view];
    [[videoTableViewCell sharedInstance].view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}




//如果cell被复用了，需要把cell上的播放器删掉
- (void)prepareForReuse{
    [super prepareForReuse];
    //判断当前cell是否有播放，如果有则删除-->自己想办法
    if ([videoTableViewCell sharedInstance].view.superview == self.touchButton) {
        [[videoTableViewCell sharedInstance].view removeFromSuperview];
        [videoTableViewCell sharedInstance].player = nil;
    }
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.imageV.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height-15);
    self.touchButton.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height-15);
    
   // self.stopButton.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height-15);
    self.labelT.frame = CGRectMake(0, self.imageV.frame.size.height-30, self.imageV.frame.size.width, 30);
    
    self.effect.frame = CGRectMake(0, 0, self.labelT.frame.size.width, self.labelT.frame.size.height);
}

-(void)setModel:(videoModel *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:_model.cover]];
    self.labelT.text = [NSString stringWithFormat:@"    %@",_model.title];
    self.str = _model.mp4_url;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
